from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

class ComponentsPages:
    def __init__(self,driver):
        self.driver = driver

    url = "http://192.168.0.102/cmrv0.5.1/v0.5.1/index.php?m=user&a=login"
    input_locator = (By.NAME, 'name')
    password_locator = (By.NAME, 'password')
    btn_locator = (By.CSS_SELECTOR, 'input[value=登录]')
    add_locator = (By.CSS_SELECTOR, '#add')
    add_group_locator = (By.CSS_SELECTOR,
                         'body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix')
    component_name_locator = (By.CSS_SELECTOR, '#title')
    selected_group_locator = (By.CSS_SELECTOR, '#widget')
    save_locator = (By.CSS_SELECTOR, 'input[value=保存]')

    def input_username(self,username):
        self.driver.find_element(*self.input_locator).send_keys(username)

    def input_password(self,password):
        self.driver.find_element(*self.password_locator).send_keys(password)

    def click_btn_login(self):
        self.driver.find_element(*self.btn_locator).click()

    def click_add_component(self):
        self.driver.find_element(*self.add_locator).click()

    def click_add_panel_component(self):
        self.driver.find_element(*self.add_group_locator).click()

    def input_component_name(self,componentname):
        self.driver.find_element(*self.component_name_locator).send_keys(componentname)

    def selection_group(self,text):
        group_element = self.driver.find_element(*self.selected_group_locator)
        group_select = Select(group_element)
        group_select.select_by_visible_text(text)

    def click_save(self):
        self.driver.find_element(*self.save_locator).click()

    def open(self):
        self.driver.get(self.url)